var wxLogin = require('/utils/wxlogin.js');
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    //this.updateLocation();
    this.doLogin();
  },
  getUserInfo:function(cb){
    var that = this
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  globalData:{
    userInfo:null,
    sysUserInfo:null,
    // apiHost: 'https://www.zyzlove.net',
    apiHost: 'http://zhzyz.app',
    subApiPath: '/api',
    appId: 'wxc15f2d494b381d04',
    apiToken: null,
    apiUser: null,
    apiAppId: 1,
    currentCityId: null,
  },
  getApiPathPrefix: function() {
    return this.globalData.apiHost + this.globalData.subApiPath;
  },
  getGlobalCity: function() {
    var cityId = wx.getStorageSync('cityId');
    try {
      if (cityId) {
        this.globalData.currentCityId = cityId;
      } else {
        this.globalData.currentCityId = 1;
      }
    } catch(e) {

    }
  },
  updateLocation:function(callback){
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res);
        var userService = require('services/userService');
        userService.getCityByPos({
          lat: res.latitude,
          lng: res.longitude,
          success: function (data) {
            that.globalData.currentCityId = data.city.id;
            callback && callback(data.city);
          },
          failure: function () {
            that.globalData.currentCityId = 1;
          }
        });
      },
    })
  },
  doLogin: function() {
    var that = this;
    wxLogin.doLogin(this, {
      success: function (data) {
        var userService = require('services/userService');

        userService.loginbyopenid({
          openid: data.openid,
          success: function (data) {
            that.globalData.apiUser = data.user_id;
            that.globalData.apiToken = data.access_token;
            that.globalData.sysUserInfo = {
              userId: data.user_info.user_id,
              avatar: data.user_info.avatar,
              nickname: data.user_info.nickname,
            };
          },
          failure: function () {

          }
        });
       
      },
      failure: function() {

      }
    });
  },
  //请求数据
  getData: function (url, params, callback) {
    wx.showLoading({ title: '加载中' });
    var dataDomain = this.globalData.apiHost + this.globalData.subApiPath; //TODO:后期修改请求地址
    var options = {
      url: dataDomain + '/' + url,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading()
        typeof callback == "function" && callback(res)
      }
    };
    console.log(params);
    if (JSON.stringify(params) != "{}") {
      options.data = params;
      options.method = "POST";
    }else{
      console.log(2);
      options.method = "GET";
    }
    wx.request(options);
  }
})